package org.example.ocp;

import org.junit.jupiter.api.Test;

/**
 * @author
 * @description 开闭原则
 */
public class Ocp2 {

    @Test
    void runTest() {
        GraphicEdito2 graphicEdito2 = new GraphicEdito2();
        graphicEdito2.drawShape(new Rectangle1());
        graphicEdito2.drawShape(new Circle1());
        graphicEdito2.drawShape(new Triangle1());
        graphicEdito2.drawShape(new OtherShape());
    }
}


//使用方 不能修改
class GraphicEdito2 {
    public void drawShape(Shape1 s1) {
      s1.draw();
    }

}

//基类
abstract class Shape1 {
    int m_type;

    public abstract void draw();

}

//提供方
class Rectangle1 extends Shape1 {
   Rectangle1() {
       super.m_type=1;
   }

    @Override
    public void draw() {
        System.out.println("画一个矩形");
    }
}

//提供方
class Circle1 extends Shape1 {
    Circle1() {
        super.m_type=2;
    }

    @Override
    public void draw() {
        System.out.println("画一个圆形");
    }
}

//提供方
class Triangle1 extends Shape1 {
    Triangle1() {
        super.m_type=3;
    }

    @Override
    public void draw() {
        System.out.println("画一个三角形");
    }
}

//提供方
class OtherShape extends Shape1 {
    OtherShape() {
        super.m_type=4;
    }

    @Override
    public void draw() {
        System.out.println("画一个其他形状");
    }
}